﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SolidWorks.Interop.sldworks;

namespace SolidworksDLL
{
    /// <summary>
    /// 属性处理的帮助类
    /// </summary>
    public class CustomPryHelp
    {

        public static CustomPropertyManager GetCustomPryMgr(ModelDoc2 swModel, string cfgName)
        {
            //自定义属性
            if (cfgName == "")
            {
                ModelDocExtension swModelDocExt = swModel.Extension;
               return swModelDocExt.get_CustomPropertyManager(""); 
            }
            else
            {
                //配置属性
                Configuration cfg = swModel.GetConfigurationByName(cfgName);
                if (null!=cfg)
                {
                    return cfg.CustomPropertyManager;
                }

            }
            return null; 
        }

        /// <summary>
        /// 获取指定配置的指定属性值
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="cfgName"></param>
        /// <param name="pryName"></param>
        /// <returns></returns>
        public static string GetPryByName(ModelDoc2 swModel,string cfgName,string pryName)
        {
            CustomPropertyManager CustomPryMgr = GetCustomPryMgr(swModel, cfgName);
            if (null!=CustomPryMgr)
            {
                string ValOut;
                string ResolvedValOut;
                bool wasResolved;

                CustomPryMgr.Get5(pryName,false, out ValOut, out ResolvedValOut, out wasResolved);
                return ResolvedValOut;
            }
            return ""; 
        }

        /// <summary>
        /// 设定指定配置属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="cfgName"></param>
        /// <param name="pryName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetPryByName(ModelDoc2 swModel, string cfgName, string pryName,string value)
        {
            CustomPropertyManager CustomPryMgr = GetCustomPryMgr(swModel, cfgName);
            if (null != CustomPryMgr)
            { 

               int result= CustomPryMgr.Set2(pryName, value);
                if (result==2)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 设定当前配置属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="pryName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetCurCfgPryByName(ModelDoc2 swModel,string pryName, string value)
        {
            string cfgName= ConfigurationHelp.GetCurCfg(swModel).Name;
            return SetPryByName(swModel, cfgName, pryName, value); 
        }

        /// <summary>
        /// 新增或者修改指定配置属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="cfgName"></param>
        /// <param name="pryName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool AddPryByName(ModelDoc2 swModel, string cfgName, string pryName, string value)
        {
            CustomPropertyManager CustomPryMgr = GetCustomPryMgr(swModel, cfgName);
            if (null != CustomPryMgr)
            {

                int result = CustomPryMgr.Add3(pryName,30, value,2);
                if (result == 0)
                {
                    return true;
                }
            }
            return false;
        }
        // <summary>
        /// 新增或者修改当前配置属性
        /// </summary>
        /// <param name="swModel"></param> 
        /// <param name="pryName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool AddCurCfgPryByName(ModelDoc2 swModel,string pryName, string value)
        {
            string cfgName = ConfigurationHelp.GetCurCfg(swModel).Name;
            return AddPryByName(swModel,cfgName,pryName,value);
        }

        /// <summary>
        /// 新增或者修改指定配置属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="cfgName"></param>
        /// <param name="pryName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool AddPryDict(ModelDoc2 swModel, string cfgName, Dictionary<string, string> pryDict)
        {
            CustomPropertyManager CustomPryMgr = GetCustomPryMgr(swModel, cfgName);
            if (null != CustomPryMgr)
            {
                int result = 0;
                foreach (var item in pryDict)
                {
                    int temp = CustomPryMgr.Add3(item.Key, 30, item.Value, 2);
                    if (temp!=0)
                    {
                        result = temp;
                    }
                }
              
                if (result == 0)
                {
                    return true;
                }
            }
            return false;
        }
        // <summary>
        /// 新增或者修改当前配置属性
        /// </summary>
        /// <param name="swModel"></param> 
        /// <param name="pryName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool AddCurCfgPryDict(ModelDoc2 swModel, Dictionary<string, string> pryDict)
        {
            string cfgName = ConfigurationHelp.GetCurCfg(swModel).Name;
            return AddPryDict(swModel, cfgName, pryDict);
        }

        /// <summary>
        /// 获取当前配置属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="pryName"></param>
        /// <returns></returns>
        public static string GetPryByName(ModelDoc2 swModel, string pryName)
        {
            string cfgName = ConfigurationHelp.GetCurCfg(swModel).Name;
            return GetPryByName(swModel, cfgName,pryName);
        }

        /// <summary>
        /// 得到指定配置所有属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="cfgName"></param>
        /// <param name="ptyDict"></param>
        public static void GetAllPryDict(ModelDoc2 swModel, string cfgName,ref Dictionary<string, string> ptyDict)
        { 
            CustomPropertyManager CustomPryMgr = GetCustomPryMgr(swModel, cfgName);
            if (null != CustomPryMgr)
            {
                //方法1 先得到所有的属性名称，然后分别取值
                string[] names=CustomPryMgr.GetNames();

                foreach (var item in names)
                {
                    string ValOut;
                    string ResolvedValOut;
                    bool wasResolved;

                    CustomPryMgr.Get5(item, false, out ValOut, out ResolvedValOut, out wasResolved);
                    ptyDict[item]=ResolvedValOut;
                }
                //方法2 直接使用GetALL2
                //object vPropNames = null;
                //string[] propNames;
                //object vPropTypes = null;
                //object vPropValues = null;
                //object[] propValues;
                //object resolved = null;

                //CustomPryMgr.GetAll2(ref vPropNames, ref vPropTypes, ref vPropValues, ref resolved);
                //propValues = (object[])resolved;
                //propNames = (string[])vPropNames;

                //if (propNames==null)
                //{
                //    return;
                //}
                //for (int i = 0; i < propNames.Length; i++)
                //{
                //    ptyDict[propNames[i]] = (string)propValues[i];
                //}

            }
        }

        /// <summary>
        /// 得到当前配置所有属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="ptyDict"></param>
        public static void GetCurCfgAllPryDict(ModelDoc2 swModel, ref Dictionary<string, string> ptyDict)
        {
            string cfgName = ConfigurationHelp.GetCurCfg(swModel).Name;
            GetAllPryDict(swModel, cfgName, ref ptyDict);
        }

        /// <summary>
        /// 删除指定配置指定属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="cfgName"></param>
        /// <param name="ptyDict"></param>
        public static void DelPry(ModelDoc2 swModel, string cfgName, string pryName)
        {
            CustomPropertyManager CustomPryMgr = GetCustomPryMgr(swModel, cfgName);
            if (null != CustomPryMgr)
            {
                string[] names = CustomPryMgr.GetNames();
                if (names==null||names.Contains(pryName)==false)
                {
                    return;
                }
                else
                {
                    CustomPryMgr.Delete2(pryName);
                }
 
            }
        }

        /// <summary>
        /// 删除当前配置属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="ptyDict"></param>
        public static void DelCurCfgAllPryDict(ModelDoc2 swModel, string pryName)
        {
            string cfgName = ConfigurationHelp.GetCurCfg(swModel).Name;
            DelPry(swModel, cfgName, pryName);
        }

        /// <summary>
        /// 清空指定配置所有属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="cfgName"></param>
        /// <param name="ptyDict"></param>
        public static void ClearPry(ModelDoc2 swModel, string cfgName)
        {
            CustomPropertyManager CustomPryMgr = GetCustomPryMgr(swModel, cfgName);
            if (null != CustomPryMgr)
            {
                string[] names = CustomPryMgr.GetNames();
                if (names == null)
                {
                    return;
                }
                else
                { 
                    foreach (var item in names)
                    {
                        CustomPryMgr.Delete2(item);
                    } 
                }

            }
        }

        /// <summary>
        /// 清空当前配置所有属性
        /// </summary>
        /// <param name="swModel"></param>
        /// <param name="ptyDict"></param>
        public static void ClearCurCfgAllPryDict(ModelDoc2 swModel)
        {
            string cfgName = ConfigurationHelp.GetCurCfg(swModel).Name;
            ClearPry(swModel, cfgName);
        }
    }
}
